#include"../common/message.hpp"
#include"../common/net.hpp"
#include"../common/dispacher.hpp"
#include"../server/rpc_router.hpp"
#include "../common/detail.hpp"
//客户端服务端的测试1
// void onRpcRequest(const bitpr::BaseConnection::ptr& conn,bitpr::RpcRequest::ptr &msg){

//     std::cout<<"收到请求Rpc"<<std::endl;

//     std::string body=msg->serialize();
//     std::cout<<body<<std::endl;
//     auto rpc_req=bitpr::MessageFactory::create<bitpr::RpcResponse>();
//     rpc_req->setId("111");
//     rpc_req->setMType(bitpr::MType::RSP_RPC);
//     rpc_req->setRCode(bitpr::RCode::RCODE_OK);
//     rpc_req->setResult(33);
//     conn->send(rpc_req);
// }
// void onTopicRequest(const bitpr::BaseConnection::ptr& conn,bitpr::TopicRequest::ptr &msg){

//     std::cout<<"收到请求Topic"<<std::endl;

//     std::string body=msg->serialize();
//     std::cout<<body<<std::endl;
//     auto rpc_req=bitpr::MessageFactory::create<bitpr::TopicResponse>();
//     rpc_req->setId("222");
//     rpc_req->setMType(bitpr::MType::RSP_TOPIC);
//     rpc_req->setRCode(bitpr::RCode::RCODE_OK);
    
//     conn->send(rpc_req);
// }

//测试2
void Add(const Json::Value &req,Json::Value &rsp){
    int num1=req["num1"].asInt();
    int num2=req["num2"].asInt();
    std::cout<<num1<<" "<<num2<<std::endl;
    rsp=num1+num2;
    ELOG("add : %d + %d = %d", num1,num2,rsp.asInt());
}
int main()
{
    auto dispacher=std::make_shared<bitpr::Dispacher>();
    //注册映射关系
    // dispacher->registerHandler<bitpr::RpcRequest>(bitpr::MType::REQ_RPC,onRpcRequest);
    // dispacher->registerHandler<bitpr::TopicRequest>(bitpr::MType::REQ_TOPIC,onTopicRequest);

    auto router=std::make_shared<bitpr::server::RpcRouter>();
    //初始化可以提供的服务
    std::unique_ptr<bitpr::server::SDescribeFactory>desc_factory(new bitpr::server::SDescribeFactory());
    desc_factory->setMethodName("Add");
    desc_factory->setParamsDesc("num1",bitpr::server::VType::INTEGRAL);
    desc_factory->setParamsDesc("num2",bitpr::server::VType::INTEGRAL);
    desc_factory->setReturntype(bitpr::server::VType::INTEGRAL);
    desc_factory->setCallback(Add);
    router->registerMethod(desc_factory->build());
    auto cb=std::bind(&bitpr::server::RpcRouter::onRpcRequest,router.get(),
    std::placeholders::_1,std::placeholders::_2);
    dispacher->registerHandler<bitpr::RpcRequest>(bitpr::MType::REQ_RPC,cb);

    auto server=bitpr::ServerFactory::create(9090);//添加端口
    auto message_cb=std::bind(&bitpr::Dispacher::onMessage,dispacher.get(),
    std::placeholders::_1,std::placeholders::_2);
    server->setMessageCallback(message_cb);
    server->start();

    return 0;
}